package com.wantupai.mvp.framework.support.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.BackstackAccessor;
import android.support.v4.app.Fragment;
import android.view.View;

import com.wantupai.mvp.framework.support.MvpDelegateCallback;
import com.wantupai.mvp.framework.support.ProxyMvpDelegateCallBack;
import com.wantupai.mvp.mvp.MvpPresenter;
import com.wantupai.mvp.mvp.MvpView;


/**
 * 第一重代理模式->生命周期->目标对象
 *
 * @param <V>
 * @param <P>
 * @author :zhangqing
 */
public class FragmentMvpDelegateImpl<V extends MvpView, P extends MvpPresenter<V>> implements FragmentMvpDelegate<V, P> {
    private ProxyMvpDelegateCallBack<V, P> mProxyMvpCallBack;
    private View view;

    public FragmentMvpDelegateImpl(MvpDelegateCallback<V, P> mvpCallback) {
        mProxyMvpCallBack = new ProxyMvpDelegateCallBack<V, P>(mvpCallback);

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        mProxyMvpCallBack.createPresenter();
        mProxyMvpCallBack.attach();
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onResume() {

    }

    @Override
    public void onStop() {

    }

    @Override
    public void onDestroyView() {
        mProxyMvpCallBack.detach();
    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {

    }

    @Override
    public void onAttach(Context context) {

    }

    @Override
    public void onDetach() {

    }

    public static boolean retainPresenterInstance(Activity activity, Fragment fragment,
                                                  boolean keepPresenterInstanceDuringScreenOrientationChanges,
                                                  boolean keepPresenterOnBackstack) {
        if (activity.isChangingConfigurations()) {
            return keepPresenterInstanceDuringScreenOrientationChanges;
        }
        if (activity.isFinishing()) {
            return false;
        }
        if (keepPresenterOnBackstack && BackstackAccessor.isFragmentOnBackStack(fragment)) {
            return true;
        }
        return !fragment.isRemoving();
    }




}
